package mapper;

import entity.Teacher;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * @author JoeZhou
 */
public interface TeacherMapper {

    /**
     * 添加教师记录
     *
     * @param teacher 教师实体
     */
    @Insert("insert into teacher (name, gender, age, info) values (#{name}, #{gender}, #{age}, #{info})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Teacher teacher);

    /**
     * 根据主键修改教师
     *
     * @param teacher 教师实体
     */
    @Update("update teacher set name = #{name}, age = #{age}, gender = #{gender}, info = #{info} where id = #{id}")
    void updateById(Teacher teacher);

    /**
     * 根据主键删除教师
     *
     * @param id 主键
     */
    @Delete("delete from teacher where id = #{id}")
    void deleteById(int id);

    /**
     * 根据主键查询教师
     *
     * @param id 主键
     * @return 对应主键的教师记录
     */
    @Select("select id, name, gender, age, info from teacher where id = #{id}")
    Teacher selectById(int id);

    /**
     * 按照姓名模糊查询所有教师
     *
     * @param name 姓名的模糊查询内容
     * @return 所有符合条件的教师记录
     */
    @Select("select id, name, gender, age, info from teacher where name like concat('%', #{name}, '%')")
    List<Teacher> selectLikeName(String name);

    /**
     * 全查教师表
     *
     * @return 全部教师记录
     */
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, javaType = Integer.class, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR, javaType = String.class),
            @Result(column = "gender", property = "gender", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
            @Result(column = "age", property = "age", jdbcType = JdbcType.INTEGER, javaType = Integer.class),
            @Result(column = "info", property = "info", jdbcType = JdbcType.VARCHAR, javaType = String.class),
    })
    @Select("select id, name, gender, age, info from teacher")
    List<Teacher> selectAll();
}